#include "commtraff_hilog.h"
#include <securec.h>
#include <string.h>

void CommtraffLogOutPrint(const char *buf, CommtraffLogLevel level)
{
    switch (level) {
        case COMMTRAFF_LOG_DBG:
            HILOG_DEBUG(COMMTRAFF_HILOG_ID, "%{public}s", buf);
            break;
        case COMMTRAFF_LOG_INFO:
            HILOG_INFO(COMMTRAFF_HILOG_ID, "%{public}s", buf);
            break;
        case COMMTRAFF_LOG_WARN:
            HILOG_WARN(COMMTRAFF_HILOG_ID, "%{public}s", buf);
            break;
        case COMMTRAFF_LOG_ERROR:
            HILOG_ERROR(COMMTRAFF_HILOG_ID, "%{public}s", buf);
            break;
        default:
            break;
    }
}


void CommtraffLog(CommtraffLogLevel level, const char *fmt, ...)
{
    char szStr[LOG_PRINT_MAX_LEN] = {0};
    va_list arg;
    if (level >= COMMTRAFF_LOG_LEVEL_MAX) {
        HILOG_ERROR(COMMTRAFF_HILOG_ID, "[CommtraffLog] log level error");
        return;
    }
    va_start(arg, fmt);
    int32_t ret = vsnprintf_s(szStr, sizeof(szStr), sizeof(szStr) - 1, fmt, arg);
    va_end(arg);
    if (ret < 0) {
        HILOG_WARN(COMMTRAFF_HILOG_ID, "[CommtraffLog] log len error");
        return;
    }
    CommtraffLogOutPrint(szStr, level);
}
